<template>
  <div class="app-container">
    <div class="handle-content">
      <el-input v-if="this.$store.getters.role.store_id == 0" v-model="storeId" placeholder="请输入店铺id" style="width: 200px;margin-right: 10px;" />
      <el-input v-model="phone" placeholder="请输入手机号" style="width: 200px;margin-right: 10px;" />
      <el-select v-model="value" placeholder="请选择用户等级">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
      <el-input v-model="userId" placeholder="请输入用户ID" style="width: 250px;">
        <el-button slot="append" @click="search">查询</el-button>
      </el-input>
      <span class="right">
        <el-button type="primary" @click="exportExcel">
          导出
        </el-button>
      </span>
    </div>

    <el-table v-loading="listLoading" :data="list" element-loading-text="拼命加载中" border fit highlight-current-row>
      <el-table-column v-if="this.$store.getters.role.store_id == 0" label="店铺ID" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.store_id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户头像" align="center">
        <template slot-scope="scope">
          <div style="max-width: 250px;text-align: center;">
            <img :src="scope.row.avatar" style="display: inline-block; width: 80%;">
          </div>
        </template>
      </el-table-column>
      <el-table-column label="用户ID" align="center">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="姓名" align="center">
        <template slot-scope="scope">
          <span style="color: rgb(64, 158, 255);cursor:pointer;" @click="handle(scope.row.id, 'true_name')">{{ scope.row.true_name == '' ? '无' : scope.row.true_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="手机" align="center">
        <template slot-scope="scope">
          <span style="color: rgb(64, 158, 255);cursor:pointer;" @click="handle(scope.row.id, 'phone')">{{ scope.row.phone }}</span>
        </template>
      </el-table-column>
      <el-table-column label="会员昵称" align="center">
        <template slot-scope="scope">
          {{ scope.row.nickname }}
        </template>
      </el-table-column>
      <el-table-column label="微信" align="center">
        <template slot-scope="scope">
          {{ scope.row.weixin == '' ? '无' : scope.row.weixin }}
        </template>
      </el-table-column>
      <el-table-column label="推荐人ID" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.pid > 1">{{ scope.row.pid }}</span>
          <span v-else>无</span>
        </template>
      </el-table-column>
      <el-table-column label="推荐人昵称" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.pid > 1">{{ scope.row.p_name }}</span>
          <span v-else>无</span>
        </template>
      </el-table-column>
      <el-table-column label="报单中心ID" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.team_id > 0">{{ scope.row.partnerId }}</span>
          <span v-else>无</span>
        </template>
      </el-table-column>
      <el-table-column label="报单中心昵称" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.team_id > 0">{{ scope.row.partner_nickname }}</span>
          <span v-else>无</span>
        </template>
      </el-table-column>
      <el-table-column label="余额" align="center" :sortable="true" :sort-method="(a,b) => sortMethod(a,b,'gold')">
        <template slot-scope="scope">
          <span style="color: rgb(64, 158, 255);cursor:pointer;" @click="handle(scope.row.id, 'gold')">{{ scope.row.gold }}</span>
        </template>
      </el-table-column>
      <el-table-column label="积分" align="center" :sortable="true" :sort-method="(a,b) => sortMethod(a,b,'jf')">
        <template slot-scope="scope">
          <span style="color: rgb(64, 158, 255);cursor:pointer;" @click="handle(scope.row.id, 'jf')">{{ scope.row.jf }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="awardName" align="center" :sortable="true" :sort-method="(a,b) => sortMethod(a,b,'jgz')">
        <template slot-scope="scope">
          <span style="color: rgb(64, 158, 255);cursor:pointer;" @click="handle(scope.row.id, 'jgz')">{{ scope.row.jgz }}</span>
        </template>
      </el-table-column>
      <el-table-column label="等级" align="center">
        <template slot-scope="scope">
          {{ scope.row.grade }}
        </template>
      </el-table-column>
      <el-table-column label="代数" align="center">
        <template slot-scope="scope">
          第{{ scope.row.level }}代
        </template>
      </el-table-column>
      <el-table-column label="是否为加美官微老用户" align="center">
        <template slot-scope="scope">
          {{ scope.row.old_user == 1 ? '是': '否' }}
        </template>
      </el-table-column>
      <el-table-column label="成为报单中心时间" align="center" :sortable="true" :sort-method="(a,b) => sortMethod(a,b,'partner_at')">
        <template slot-scope="scope">
          {{ scope.row.partner_at }}
        </template>
      </el-table-column>
      <el-table-column label="注册时间" align="center" :sortable="true" :sort-method="(a,b) => sortMethod(a,b,'created_at')">
        <template slot-scope="scope">
          {{ scope.row.created_at }}
        </template>
      </el-table-column>
      <el-table-column label="升级报单中心" align="center">
        <template slot-scope="scope">
          <el-button title="操作" type="primary" circle icon="el-icon-edit" @click="handle(scope.row.id, 'team_id')" />
        </template>
      </el-table-column>
    </el-table>
    <pagination v-if="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.pageSize" @pagination="getList" />
    <member-handle ref="memberHandle" @afterSave="afterSave" />
  </div>
</template>

<script>
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import MemberHandle from './component/handle'
import { getList } from '@/api/member'
import { getToken } from '@/utils/auth' // get token from cookie
export default {

  components: { Pagination, MemberHandle },

  data() {
    return {
      list: [],
      listLoading: false,
      listQuery: {
        page: 1,
        storeId: 0,
        userId: '',
        phone: '',
        grade: '',
        pageSize: 10
      },
      total: 0,
      storeId: '',
      phone: '',
      userId: '',
      path: '',
      value: '',
      awardName: '订单指数',
      options: [{
          value: '',
          label: '全部'
        },{
          value: '0',
          label: '会员'
        }, {
          value: '1',
          label: 'VIP会员'
        }, {
          value: '2',
          label: '报单中心'
        }],
    }
  },

  mounted() {
    this.getList()
  },

  methods: {
    getList() {
      this.listLoading = true
      getList(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total
        this.listLoading = false
        this.path = response.data.path
        this.awardName = response.data.awardName
      })
    },

    search() {
      this.listQuery.storeId = this.storeId
      this.listQuery.userId = this.userId
      this.listQuery.phone = this.phone
      this.listQuery.grade = this.value
      this.getList()
    },

    handle(id, type) {
      this.$refs.memberHandle.handle(id, type, this.awardName)
    },

    afterSave() {
      this.getList()
    },

    exportExcel() {
      const url = this.path + '?storeId=' + this.listQuery.storeId + '&userId=' + this.listQuery.userId + '&phone=' + this.listQuery.phone + '&grade=' + this.listQuery.grade + '&pageSize=' + this.listQuery.pageSize + '&page=' + this.listQuery.page + '&token=' + getToken() + '&report=' + true

      location.href = url
    },

    sortMethod(obj1, obj2, column){
      console.log(obj1, obj2, column)
      var at = obj1[column]
      var bt = obj2[column]
      console.log(at, bt)
      if(at > bt){
          return 1
      }else{
        return -1
      }
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.app-container{
    .handle-content{
        margin-bottom: 20px;
        .right{
            float: right;
        }
    }
}
</style>
<style>
.el-table .warning-row {
}

.el-table .success-row {
}
</style>
